class GameController < ApplicationController
  
  before_filter :find_game, :find_teams, :find_score, :find_time_left
  
  def index
    if session[:user]
      redirect_to :controller => 'home', :action => 'signed_in'
    else
      redirect_to :controller => 'home', :action => 'index'
    end
  end 
  
  def begin 
    @current_down      = @game.current_down
    @first_downs_to_go = @game.to_go
    @player_1_score    = @score.player_1_score
    @player_2_score    = @score.player_2_score
    @player1           = session[:user]#@teams.get_team(0)
    @picks1            = @player1.players
    @player2           = @teams.get_team(1)
    @picks2            = @player2.players
    @offense           = @picks1.select {|pick| pick.position == "QB" || pick.position == "WR"}
    @defense           = @picks2.select {|pick| pick.position == "CB" || pick.position == "DL"}
    @QB                = @picks1.select {|pick| pick.position == "QB"}.first
    @WR                = @picks1.select {|pick| pick.position == "WR"}.first
    @DL                = @picks2.select {|pick| pick.position == "DL"}.first
    @CB                = @picks2.select {|pick| pick.position == "CB"}.first
    @rand1             = rand(@QB.overall) + 21
    @rand2             = rand(@WR.overall) + 11
    @rand3             = rand(@DL.overall) + 1
    @rand4             = rand(@CB.overall) + 1
    while @rand1 == @rand3
      @rand1 = rand(@QB.overall) + 21
      @rand3 = rand(@DL.overall) + 1
    end
    while @rand2 == @rand4 
      @rand2             = rand(@WR.overall) + 11
      @rand4             = rand(@CB.overall) + 1
    end
    if @rand1 > @rand3
      if @rand2 > @rand4
        @outcome = "FIRST DOWN!"
        @current_down = @game.first_down
        if @first_downs_to_go == 1
          @score.touchdown_1
          redirect_to :action => 'touchdown_1'
        end
        @first_downs_to_go = @game.decrement_first_downs
      end
    end
    if @rand3 > @rand1
      if @rand4 > @rand2
        if @current_down == "4th"
          session[:game] = nil
          redirect_to :action => 'turnover_on_downs'
        end
        @sack = "SACK!"
        @current_down = @game.find_down(@current_down)
        @first_downs_to_go = @game.increment_first_downs
      end
    end
    if @rand1 > @rand3
      if @rand4 > @rand2
        if @current_down == "4th"
          session[:game] = nil
          redirect_to :action => 'turnover_on_downs'
        end
        @outcome = "INCOMPLETE!"
        @current_down = @game.find_down(@current_down)
      end
    end
    if @rand3 > @rand1
      if @rand2 > @rand4
        if @current_down == "4th"
          session[:game] = nil
          redirect_to :action => 'turnover_on_downs'
        end
        @outcome = "INCOMPLETE!"
        @current_down = @game.find_down(@current_down)
      end
    end
    @current_quarter = @time.quarter
    @drives_left     = @time.drives
  end
  
  def switch
    @current_down      = @game.current_down
    @first_downs_to_go = @game.to_go
    @player_1_score    = @score.player_1_score
    @player_2_score    = @score.player_2_score
    @player1           = session[:user]#@teams.get_team(0)
    @picks1            = @player1.players
    @player2           = @teams.get_team(1)
    @picks2            = @player2.players
    @offense           = @picks2.select {|pick| pick.position == "QB" || pick.position == "WR"}
    @defense           = @picks1.select {|pick| pick.position == "CB" || pick.position == "DL"}
    @QB                = @picks2.select {|pick| pick.position == "QB"}.first
    @WR                = @picks2.select {|pick| pick.position == "WR"}.first
    @DL                = @picks1.select {|pick| pick.position == "DL"}.first
    @CB                = @picks1.select {|pick| pick.position == "CB"}.first
    @rand1             = rand(@QB.overall) + 20  
    @rand2             = rand(@WR.overall) + 11
    @rand3             = rand(@DL.overall) + 1
    @rand4             = rand(@CB.overall) + 1
    while @rand1 == @rand3
      @rand1 = rand(@QB.overall) + 21
      @rand3 = rand(@DL.overall) + 1
    end
    while @rand2 == @rand4 
      @rand2             = rand(@WR.overall) + 11
      @rand4             = rand(@CB.overall) + 1
    end
    if @rand1 > @rand3
      if @rand2 > @rand4
        @outcome = "FIRST DOWN!"
        @current_down = @game.first_down
        if @first_downs_to_go == 1
          @score.touchdown_2
          redirect_to :action => 'touchdown_2'
        end
        @first_downs_to_go = @game.decrement_first_downs
      end
    end
    if @rand3 > @rand1
      if @rand4 > @rand2
        if @current_down == "4th"
          session[:game] = nil
          redirect_to :action => 'turnover_on_downs_2'
        end
        @sack = "SACK!"
        @current_down = @game.find_down(@current_down)
        @first_downs_to_go = @game.increment_first_downs
      end
    end
    if @rand1 > @rand3
      if @rand4 > @rand2
        if @current_down == "4th"
          session[:game] = nil
          redirect_to :action => 'turnover_on_downs_2'
        end
        @outcome = "INCOMPLETE!"
        @current_down = @game.find_down(@current_down)
      end
    end
    if @rand3 > @rand1
      if @rand2 > @rand4
        if @current_down == "4th"
          session[:game] = nil
          redirect_to :action => 'turnover_on_downs_2'
        end
        @outcome = "INCOMPLETE!"
        @current_down = @game.find_down(@current_down)
      end
    end
    @current_quarter = @time.quarter
    @drives_left     = @time.drives
  end
  
  def draw
    @player_1_score    = @score.player_1_score
    @player_2_score    = @score.player_2_score
    @player1           = session[:user]#@teams.get_team(0)
    @picks1            = @player1.players
    @player2           = @teams.get_team(1)
    @picks2            = @player2.players
    @offense           = @picks1.select {|pick| pick.position == "QB" || pick.position == "WR"}
    @defense           = @picks2.select {|pick| pick.position == "CB" || pick.position == "DL"}
    @QB                = @picks1.select {|pick| pick.position == "QB"}.first
    @WR                = @picks1.select {|pick| pick.position == "WR"}.first
    @DL                = @picks2.select {|pick| pick.position == "DL"}.first
    @CB                = @picks2.select {|pick| pick.position == "CB"}.first
    @current_down      = "1st"
    @first_downs_to_go = 5
    @current_quarter   = @time.quarter
    @drives_left       = @time.drives
  end
  
  def draw_2
    @player_1_score    = @score.player_1_score
    @player_2_score    = @score.player_2_score
    @player1           = @teams.get_team(1)
    @picks1            = @player1.players
    @player2           = @teams.get_team(0)
    @picks2            = @player2.players
    @offense           = @picks1.select {|pick| pick.position == "QB" || pick.position == "WR"}
    @defense           = @picks2.select {|pick| pick.position == "CB" || pick.position == "DL"}
    @QB                = @picks1.select {|pick| pick.position == "QB"}.first
    @WR                = @picks1.select {|pick| pick.position == "WR"}.first
    @DL                = @picks2.select {|pick| pick.position == "DL"}.first
    @CB                = @picks2.select {|pick| pick.position == "CB"}.first
    @current_down      = "1st"
    @first_downs_to_go = 5
    @current_quarter   = @time.quarter
    @drives_left       = @time.drives
  end
  
  def kick_field_goal_1
    @first_downs_to_go   = @game.to_go
    @player1             = session[:user]#@teams.get_team(0)
    @picks1              = @player1.players
    @kicker              = @picks1.select {|player| player.position == "K"}.first
    @random              = (rand(@kicker.overall) + 1) * 2
    if @first_downs_to_go == 3
      if @random >= 130
        @score.field_goal_1
        @result = "KICK IS GOOD!!"
      else
        @result = "NO GOOD!!"
      end
    end
    if @first_downs_to_go == 2
      if @random >= 90
        @score.field_goal_1
        @result = "KICK IS GOOD!!"
      else
        @result = "NO GOOD!!"
      end
    end
    if @first_downs_to_go == 1
      if @random >= 50
        @score.field_goal_1
        @result = "KICK IS GOOD!!"
      else
        @result = "NO GOOD!!"
      end
    end
    @player_1_score = @score.player_1_score
    @player_2_score = @score.player_2_score
    session[:game] = nil
  end
  
  def kick_field_goal_2
    @first_downs_to_go   = @game.to_go
    @player1             = @teams.get_team(1)
    @picks1              = @player1.players
    @kicker              = @picks1.select {|player| player.position == "K"}.first
    @random              = (rand(@kicker.overall) + 1) * 2
    if @first_downs_to_go == 3
      if @random >= 130
        @score.field_goal_2
        @result = "KICK IS GOOD!!"
      else
        @result = "NO GOOD!!"
      end
    end
    if @first_downs_to_go == 2
      if @random >= 90
        @score.field_goal_2
        @result = "KICK IS GOOD!!"
      else
        @result = "NO GOOD!!"
      end
    end
    if @first_downs_to_go == 1
      if @random >= 50
        @score.field_goal_2
        @result = "KICK IS GOOD!!"
      else
        @result = "NO GOOD!!"
      end
    end
    @player_1_score  = @score.player_1_score
    @player_2_score  = @score.player_2_score
    @drives_left = @time.decrement_drives_left
    session[:game]   = nil
    if @drives_left == 0
      @end = ""
      if @current_quarter == "4th"
        redirect_to :action => 'win'
      elsif @current_quarter == "OT"
        redirect_to :action => 'overtime'
      else
      end
    end
  end
  
  def touchdown_1
    @player_1_score = @score.player_1_score
    @player_2_score = @score.player_2_score
    session[:game]  = nil
  end
  
  def touchdown_2
    @player_1_score = @score.player_1_score
    @player_2_score = @score.player_2_score
    session[:game] = nil
  end
  
  def turnover_on_downs
    @player_1_score = @score.player_1_score
    @player_2_score = @score.player_2_score
    session[:game] = nil
  end
  
  def turnover_on_downs_2
    @player_1_score = @score.player_1_score
    @player_2_score = @score.player_2_score
    session[:game] = nil
    @drives_left = @time.decrement_drives_left
    if @drives_left == 0
      @end = ""
      if @current_quarter == "4th"
        redirect_to :action => 'win'
      elsif @current_quarter == "OT"
        redirect_to :action => 'overtime'
      else
      end
    end
  end
  
  def extra_point_1
     @first_downs_to_go   = @game.to_go
     @player1             = session[:user]#@teams.get_team(0)
     @picks1              = @player1.players
     @kicker              = @picks1.select {|player| player.position == "K"}.first
    if (rand(100) + 1) > 4
      @score.extra_point_1
      @result = "PAT GOOD!!"
    else
      @result = "NO GOOD!!"
    end
    @player_1_score = @score.player_1_score
    @player_2_score = @score.player_2_score
    render :kick_field_goal_1
  end
  
  def extra_point_2
    @first_downs_to_go   = @game.to_go
    @player1             = @teams.get_team(1)
    @picks1              = @player1.players
    @kicker              = @picks1.select {|player| player.position == "K"}.first
    if (rand(100) + 1) > 4
      @score.extra_point_2
      @result = "KICK IS GOOD!!"
    else
      @result = "NO GOOD!!"
    end
    @player_1_score = @score.player_1_score
    @player_2_score = @score.player_2_score
    @drives_left = @time.decrement_drives_left
    session[:game]   = nil
    if @drives_left == 0
      @end = ""
      if @current_quarter == "4th"
        redirect_to :action => 'win'
      elsif @current_quarter == "OT"
        redirect_to :action => 'overtime'
      else
        render :kick_field_goal_2
      end
    else
      render :kick_field_goal_2
    end
  end
  
  def two_point_1
    @first_downs_to_go   = @game.to_go
    @player1             = session[:user]#@teams.get_team(0)
    @picks1              = @player1.players
    @kicker              = @picks1.select {|player| player.position == "K"}.first
    if (rand(100) + 1) > 65
      @score.two_point_1
      @result = "2-POINT CONVERSION GOOD!!"
    else
      @result = "NO GOOD!!"
    end
    @player_1_score = @score.player_1_score
    @player_2_score = @score.player_2_score
    render :kick_field_goal_1
  end
  
  def two_point_2
    @first_downs_to_go   = @game.to_go
    @player1             = @teams.get_team(1)
    @picks1              = @player1.players
    @kicker              = @picks1.select {|player| player.position == "K"}.first
    if (rand(100) + 1) > 65
      @score.two_point_2
      @result = "2-POINT CONVERSION GOOD!!"
    else
      @result = "NO GOOD!!"
    end
    @player_1_score = @score.player_1_score
    @player_2_score = @score.player_2_score
    @drives_left = @time.decrement_drives_left
    session[:game]   = nil
    if @drives_left == 0
      @end = ""
      if @current_quarter == "4th" 
        redirect_to :action => 'win'
      elsif @current_quarter == "OT"
        redirect_to :action => 'overtime'
      else
        render :kick_field_goal_2
      end
    else
      render :kick_field_goal_2
    end
  end
  
  def end_of_quarter
    @drives_left = @time.reset 
    if @current_quarter == "1st"
      @score.set_first_quarter
    elsif @current_quarter == "2nd"
      @score.set_second_quarter
    elsif @current_quarter == "3rd"
      @score.set_third_quarter
    elsif @current_quarter == "4th"
      @score.set_fourth_quarter
    end
    @current_quarter = @time.increment_quarter
    @score.reset_current_scores
    @player_1_first_quarter_score  = @score.player_1_first_quarter_score 
    @player_2_first_quarter_score  = @score.player_2_first_quarter_score
    @player_1_second_quarter_score = @score.player_1_second_quarter_score
    @player_2_second_quarter_score = @score.player_2_second_quarter_score
    @player_1_third_quarter_score  = @score.player_1_third_quarter_score
    @player_2_third_quarter_score  = @score.player_2_third_quarter_score
    @player_1_fourth_quarter_score = @score.player_1_fourth_quarter_score
    @player_2_fourth_quarter_score = @score.player_2_fourth_quarter_score
  end
  
  def win
    @player_1 = session[:user]
    @player_2 = @teams.get_team(1)
    @score.set_fourth_quarter
    @score.reset_current_scores
    @player_1_first_quarter_score  = @score.player_1_first_quarter_score 
    @player_2_first_quarter_score  = @score.player_2_first_quarter_score
    @player_1_second_quarter_score = @score.player_1_second_quarter_score
    @player_2_second_quarter_score = @score.player_2_second_quarter_score
    @player_1_third_quarter_score  = @score.player_1_third_quarter_score
    @player_2_third_quarter_score  = @score.player_2_third_quarter_score
    @player_1_fourth_quarter_score = @score.player_1_fourth_quarter_score
    @player_2_fourth_quarter_score = @score.player_2_fourth_quarter_score
    @total_1 = @player_1_first_quarter_score + @player_1_second_quarter_score + @player_1_third_quarter_score + @player_1_fourth_quarter_score
    @total_2 = @player_2_first_quarter_score + @player_2_second_quarter_score + @player_2_third_quarter_score + @player_2_fourth_quarter_score
    if @total_1 == @total_2
      @time.increment_quarter
      redirect_to :action => 'overtime_intro'
    elsif @total_1 > @total_2
      @player_1.wins  += 1
      @player_2.loses += 1
    else
      @player_1.loses += 1
      @player_2.wins  += 1
    end
    @player_1.save
    @player_2.save
    if @total_1 != @total_2
      session[:team]    = nil
      session[:game]    = nil
      session[:score]   = nil
      session[:quarter] = nil
    end
  end
  
  def overtime_intro
    @drives_left = @time.reset
  end 
  
  def overtime
    @player_1 = session[:user]
    @player_2 = @teams.get_team(1)
    @score.set_OT
    @player_1_first_quarter_score  = @score.player_1_first_quarter_score 
    @player_2_first_quarter_score  = @score.player_2_first_quarter_score
    @player_1_second_quarter_score = @score.player_1_second_quarter_score
    @player_2_second_quarter_score = @score.player_2_second_quarter_score
    @player_1_third_quarter_score  = @score.player_1_third_quarter_score
    @player_2_third_quarter_score  = @score.player_2_third_quarter_score
    @player_1_fourth_quarter_score = @score.player_1_fourth_quarter_score
    @player_2_fourth_quarter_score = @score.player_2_fourth_quarter_score
    @total_1 = @player_1_first_quarter_score + @player_1_second_quarter_score + @player_1_third_quarter_score + @player_1_fourth_quarter_score
    @total_2 = @player_2_first_quarter_score + @player_2_second_quarter_score + @player_2_third_quarter_score + @player_2_fourth_quarter_score
    @player_1_OT_score = @score.player_1_OT_score
    @player_2_OT_score = @score.player_2_OT_score
    if @player_1_OT_score > @player_2_OT_score
      @player_1.wins  += 1
      @player_2.loses += 1
      session[:team]    = nil
      session[:game]    = nil
      session[:score]   = nil
      session[:quarter] = nil
    elsif @player_2_OT_score > @player_1_OT_score
      @player_1.loses += 1
      @player_2.wins  += 1
      session[:team]    = nil
      session[:game]    = nil
      session[:score]   = nil
      session[:quarter] = nil
    else
      redirect_to :action => 'overtime_intro' 
    end
    @player_1.save
    @player_2.save
  end
      
    
private

  def find_teams
    @teams = (session[:team] ||= Team.new)
  end
  
  def find_game
    @game = (session[:game] ||= Game.new)
  end
  
  def find_score
    @score = (session[:score] ||= Score.new)
  end
  
  def find_time_left
    @time            = (session[:quarter] ||= Quarter.new)
    @drives_left     = @time.drives
    @current_quarter = @time.quarter      
  end
end
